package com.yeyks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.yeyks.cnbooking.Result.addresssearch.CbAddressSearchData;
import com.yeyks.cnbooking.Result.addresssearch.CbCountry;
import com.yeyks.cnbooking.base.CNResponse;
import com.yeyks.cnbooking.param.addresssearch.CbAddressSearchParam;
import com.yeyks.cnbooking.service.CNBookingService;
import com.yeyks.cnbooking.service.CnBookingAddressService;
import com.yeyks.other.dal.domain.AddressCode;
import com.yeyks.other.dal.domain.ImportCityDataConfig;
import com.yeyks.other.service.ImportCityDataConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: dagny
 * @date: 2019/10/31 0031 16:58
 */
@Api(tags = "CnBookingAddressController_CN的地址操作")
@RestController
@RequestMapping("/cn/address")
public class CnBookingAddressController {

    @Autowired
    private CNBookingService cnBookingService;

    @Autowired
    private CnBookingAddressService cnBookingAddressService;

    @Autowired
    private ImportCityDataConfigService importCityDataConfigService;

    @ApiOperation("新增地址信息")
    @PostMapping("/add")
    public int addCNBookingAddress(){

        CbAddressSearchParam param = new CbAddressSearchParam();
        param.setActionName("CityQuery");
        CNResponse<CbAddressSearchData> cbAddressSearchDataCNResponse = cnBookingService.addressSearch(param);
        CbAddressSearchData data = cbAddressSearchDataCNResponse.getData();
        if(null != data && null != data.getCountrys() && !CollectionUtils.isEmpty(data.getCountrys().getCountry())){
            List<CbCountry> cbCountries = data.getCountrys().getCountry();
            CbCountry cbCountryDB = null;
            for(CbCountry cbCountry : cbCountries){
                if("0001".equals(cbCountry.getCountryId())){
                    cbCountryDB = cbCountry;
                    break;
                }
            }
            cnBookingAddressService.addAddressCode(cbCountryDB);
        }

        return 0;
    }

    @CrossOrigin
    @ApiOperation("查询地址信息")
    @PostMapping("/list")
    public List<AddressCode> queryCnAddress(@RequestParam(value = "parentId", required = false, defaultValue = "") String parentId){

        List<AddressCode> addressCodeList  =  cnBookingAddressService.queryCnAddress(parentId);
        if(null != addressCodeList){
            return addressCodeList;
        }
        return null;
    }

    @CrossOrigin
    @ApiOperation("添加录入地址信息")
    @PostMapping("/add/import")
    public int addImportAddress(
            @RequestParam(value = "provinceId") String provinceId,
            @RequestParam(value = "cityIds") String cityIds){

        List<ImportCityDataConfig> importCityDataConfigs = cnBookingAddressService.queryList(provinceId, cityIds);
        if(!CollectionUtils.isEmpty(importCityDataConfigs)){
            boolean save = importCityDataConfigService.saveBatch(importCityDataConfigs);
            if (save){
                return 1;
            }
        }
        return 0;
    }

}
